package com.nurese.controller;


import com.nurese.entity.*;
import com.nurese.entity.Evaluate;
import com.nurese.service.*;

import com.nurese.service.EvaluateService;
import com.nurese.utils.MapControll;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/evaluate")
public class EvaluateController {

    private final String LIST="Evaluate/list";
    private final String ADD="Evaluate/add";
    private final String UPDATE="Evaluate/update";

    @Autowired
    private UserService userService;
    @Autowired
    private CareworkerService careworkerService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private EvaluateService evaluateService;


    @GetMapping("/add")
    public String create(ModelMap modelMap){
        List<Order> orders=orderService.query(null);
        modelMap.addAttribute("orders",orders);
        return ADD;
    }

    @PostMapping("/create")
    @ResponseBody
//    public void  create(Evaluate evaluate){
//        evaluateService.create(evaluate);
//    }
    public Map<String,Object> create(Evaluate evaluate){
//        evaluateService.create(evaluate);
        int result= evaluateService.create(evaluate);
        if (result<=0){
//            新增失败
            return MapControll.getInstance().error().getMap();

        }
        return MapControll.getInstance().success().getMap();
    }

    @PostMapping("/delete/{id}")
    @ResponseBody
    public Map<String,Object> delete(@PathVariable("id") Integer id){
        int result = evaluateService.delete(id);
        if(result<=0){
            return MapControll.getInstance().error().getMap();
        }
        return MapControll.getInstance().success().getMap();
    }
    @PostMapping("/delete")
    @ResponseBody
    public Map<String,Object> delete(String ids){
        int result = evaluateService.delete(ids);
        if(result<=0){
            return MapControll.getInstance().error().getMap();
        }
        return MapControll.getInstance().success().getMap();
    }


    @PostMapping("/update")
    @ResponseBody
//    @RequestBody
    public Map<String,Object> update(Evaluate evaluate){
        System.out.println("======22"+evaluate);

        int result = evaluateService.update(evaluate);
        if(result<=0){
            return MapControll.getInstance().error().getMap();
        }
        return MapControll.getInstance().success().getMap();
    }


    @GetMapping ("/detail/{id}")
    public String detail(@PathVariable("id") Integer id, ModelMap modelMap){
        List<Order> orders=orderService.query(null);
        modelMap.addAttribute("orders",orders);
        Evaluate evaluate=evaluateService.detail(id);
        modelMap.addAttribute("evaluate",evaluate);
        return UPDATE;
    }


    @PostMapping("/query")
    @ResponseBody
    public Map<String,Object> query(Evaluate evaluate){
        List<Evaluate> evaluates =evaluateService.query(evaluate);
        List<Order> orders =orderService.query(null);

        evaluates.forEach(entity->{

            orders.forEach(order -> {
                if(order.getId() == entity.getOrderid().intValue()){
                    entity.setOrder(order);
                }
            });
        });


        Integer count=evaluateService.count(evaluate);
        return MapControll.getInstance().success().put("data",evaluates).put("count",count).getMap();
    }

    @GetMapping("/list")
    public String list(){

        return LIST;
    }
}
